<?php 

Class formsModel extends appModel
{

	public function forms_list($page,$sort)
	{
		$sql="SELECT id,name,is_enable,created FROM ".$this->table('forms')."";
		$result=$this->show_page($sql,$page);
		if($result)
		{
		foreach($result['result'] as  $k=>$v)
		{
		
			$result['result'][$k]['name']=hy_dig_filter($v['name'],1);
			
		} 
		}
		$listPage['pagelink']=$result['page'];
	    $listPage['data']=$result['result'];
	    return $listPage;
	}
	public function log_list($id,$page,$sort)
	{
		$sql="SELECT * FROM ".$this->table('forms_log_'.$id)."";
		$result=$this->show_page($sql,$page);
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
		return $listPage;
	}
	public function get_all_forms()
	{
		$sql="SELECT name,id FROM ".$this->table('forms')."";
		$result=$this->getAll($sql);
		if($result)
		{
			foreach($result as  $k=>$v)
			{
				$result[$k]['name']=hy_dig_filter($v['name'],1);
			}
		}
		return $result;
	}
	public function create_forms($info)
	{
		$insert_id=$info['id'];
	
		$fields=array();
		$sql="
CREATE TABLE ".$this->table('forms_log').'_'.$insert_id." (
  `id` int(11) unsigned NOT NULL auto_increment,";
		$arr=explode('::', $info['fields']) ;//print_r($arr[1]);
		foreach($arr as $k=>$v)
		{
			$temp=json_decode(stripcslashes($v));
			
			if($temp->k>0)
			{
				$_sql="SELECT data,type  FROM ".$this->table('fields')." WHERE id='".$temp->k."'";
				$fields=$this->getRow($_sql);
				$sql.=" `".$fields['data']."` text(0) NOT NULL default '',";
			}
		}
		
		$sql.=" PRIMARY KEY  (`id`)
)  ENGINE=MyISAM  DEFAULT CHARSET=utf8";
		return $this->query($sql);
		
	}
	public function get_field($id)
	{
		if($id)
		{
			$sql="SELECT a.*  FROM ".$this->table('fields')." as a WHERE a.id = '".$id."'";
			$result=$this->getRow($sql);
			if($result)
			{
				$arr=hy_dig_filter($result['name'],3);
				$arr2=hy_dig_filter($result['default_val'],3);
				$arr3=hy_dig_filter($result['tips'],3);
				$arr4=hy_dig_filter($result['field'],3);
				$result['en_tips']=stripslashes($arr3[1]);
				$result['tips']=stripslashes($arr3[0]);
				$result['en_field']=explode('::',$arr4[1]);
				$result['field']=explode('::',$arr4[0]);
				$result['en_name']=$arr[1];
				$result['name']=$arr[0];
				$result['en_default']=$arr2[1];
				$result['default']=$arr2[0];
				
				if(is_array($result['field']))
				{   $temp=array();
					foreach($result['field'] as $k=>$v)
					{
						$temp['field']=$v;
						$v=json_decode($v);
						$temp['key']=$v->k;
						$temp['val']=$v->v;
						$temp['thumb']=$v->t;
						$result['field'][$k]=$temp;
					}
				}
				
				if(is_array($result['en_field']))
				{   $temp=array();
					foreach($result['en_field'] as $k=>$v)
					{
						$temp['field']=$v;
						$v=json_decode($v);
						$temp['key']=$v->k;
						$temp['val']=$v->v;
						$temp['thumb']=$v->t;
						$result['en_field'][$k]=$temp;
					}
				}
				
				//$result['field']=$result['field']
				return $result;
			}
			else
			{
				return $result=array();
			}
		}
	}
	public function get_all_field()
	{
		$sql="SELECT a.id,a.name,a.type,a.data FROM (".$this->table('fields')." as a)  ORDER BY a.id DESC";
		$res=$this->getAll($sql);
		foreach($res as  $k=>$v)
		{
		  $res[$k]['name']=hy_dig_filter($v['name'],1);
		}
		return $res;
	}
	public function get_field_list($page=1,$sort=array())
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where=' 1=1';
		
		if($sort['keyword']!='')
		{
			$where.=" AND title like '%".$sort['keyword']."%'";
		}
		$sql="SELECT a.id,a.name,a.type,a.data FROM (".$this->table('fields')." as a)  WHERE ".$where." ORDER BY a.id DESC";
		$result=$this->show_page($sql,$page);
		
		if($result)
		{
			foreach($result['result'] as  $k=>$v)
			{
				switch($v['type'])
				{
					case 0:$result['result'][$k]['type']='单行文本';break;
					case 1:$result['result'][$k]['type']='多行文本';break;
					case 2:$result['result'][$k]['type']='单选按钮';break;
					case 3:$result['result'][$k]['type']='多选按钮';break;
					case 4:$result['result'][$k]['type']='下拉列表';break;
					case 5:$result['result'][$k]['type']='上传';break;
				}
				$result['result'][$k]['name']=hy_dig_filter($v['name'],1);
					
			}
		}
		return $result;
	}
}